<?php
/*
 * СПВ
 **/
class SP_Common_Logger
{
	private $log_folder;
	private $full_path;
	
	function __construct()
	{
		$this->log_folder='/log/';
		$this->full_path=get_include_path();
	}
	
	public function setLogFolder($name)
	{
		$this->log_folder=$name;
	}
	
	public function setFullPath($path)
	{
		$this->full_path=$path;
	}
	
	public function databaseLog($msg, $prefix='db')
	{
		$this->logToFile($msg, $set_time=true, $prefix.'_'.date("Y-m-d").".log");
	}
	
	public function logToFile($msg, $set_time=true, $file_name=NULL)
	{
		if (!$file_name) $file_name=date("Y-m-d").".log";
		$file_name=$this->full_path.$this->log_folder.$file_name;
		if( $set_time ) $msg=date('H:i:s')."\t".$msg;
		$msg.="\r\n";
		$handle = @fopen($file_name, "a");
		@fwrite($handle, $msg);
		@fclose($handle); 
	}
	
	public function arrayToFile($array, $set_time=true, $file_name=NULL)
	{
		$msg='';
		foreach($array as $value)
		{
			$msg=$msg.$value."\t";
		}
		$this->logToFile($msg, $set_time, $file_name);
	}
	
	public function logToBase($base,$table,$array)
	{
			$fields=SP_Common_Functions::arrayToString($array,',','',true);
			$values=SP_Common_Functions::arrayToString($array,',',"'");
			$query="INSERT INTO $table ($fields) VALUES ($values)";
			$base->getWriter()->Query($query);		
	}
}
?>